/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Contronler;

import Connect.ConnectionJDBC;
import DAO.UserDAO;
import DatabasePOJO.user;
import MD5.MD5;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author miayeu
 */
@WebServlet(name = "DangKi", urlPatterns = {"/DangKi"})
public class DangKi extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
     public static int tinhTrangHonNhan(String tinhTrangHonNhan){
        if(tinhTrangHonNhan.equals("Đã kết hôn"))
        {
            return 1;
        }
        return 0;
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
               
           String tenDangNhap=request.getParameter("data[User][username]") ;
           String matKhau=request.getParameter("data[User][passwd]");
           String soDiDong=request.getParameter("data[UserProfile][phonenumber]");
           String email=request.getParameter("data[User][email]");
           String ho=request.getParameter("data[UserProfile][last_name]");;
           String tenDem=request.getParameter("data[UserProfile][middle_name]");;
           String ten=request.getParameter("data[UserProfile][first_name]");;
           String tenThat=ho+" "+tenDem+" "+ten;
           String gioiTinh=request.getParameter("data[UserProfile][gender_id]");
           String ngaySinh=request.getParameter("dob_text");
           String diachi=request.getParameter("data[UserProfile][address]")+" "+request.getParameter("data[UserProfile][city_id]");
           String tinhTrangHonNhan=request.getParameter("data[UserProfile][marriage]");
           
          
          
           user kh=new user();
           kh.setTenDangNhap(tenDangNhap);
           kh.setMatKhau(matKhau);
           kh.setDienThoai(soDiDong);
           kh.setEmail(email);
           kh.setTenThat(tenThat);
           kh.setGioTinh(gioiTinh);
           kh.setNgaySinh(ngaySinh);
           kh.setDiaChi(diachi);
           kh.setTinhTrangHonNhan(tinhTrangHonNhan);
           
               boolean n=UserDAO.dangKy(kh);
               if(n==true){
                    HttpSession session=request.getSession();
                    if(session.getAttribute("KhachHang")==null){
                       session.setAttribute("KhachHang", kh);
                   }else{
                        session.removeAttribute("KhachHang");
                         session.setAttribute("KhachHang", kh);
                    }
                  
                   out.print("<a href='index.jsp'>Bạn Đã Đăng Kí Thanh CÔng,Bấm vào đây để về trang chủ</a>");

               }else{
                   
                    out.print("<a href='index.jsp'>Bạn Đã Đăng Kí không Thanh CÔng,Bấm vào đây để về trang chủ</a>");
               }
      
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(DangKi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(DangKi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
